package com.molichuxing.gateway.bff.goods.services.impl;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.utils.ObjectCastUtil;
import com.molichuxing.gateway.bff.goods.convertor.Convert;
import com.molichuxing.gateway.bff.goods.entity.request.create.GoodsExclusiveCreateVo;
import com.molichuxing.gateway.bff.goods.entity.request.create.GoodsSubscribeCreateVo;
import com.molichuxing.gateway.bff.goods.entity.response.GoodsBaseVo;
import com.molichuxing.gateway.bff.goods.entity.response.GoodsDealerVo;
import com.molichuxing.gateway.bff.goods.entity.response.GoodsDetailVo;
import com.molichuxing.gateway.bff.goods.entity.response.GoodsVo;
import com.molichuxing.gateway.bff.goods.services.GoodsAuthBffService;
import com.molichuxing.gateway.bff.goods.services.GoodsBffService;
import com.molichuxing.services.business.dto.response.GoodsBizBizDto;
import com.molichuxing.services.business.service.GoodsBizService;
import com.molichuxing.services.infrastructure.dto.response.*;
import com.molichuxing.services.infrastructure.service.DealerService;
import com.molichuxing.services.infrastructure.service.GoodsAuthService;
import com.molichuxing.services.infrastructure.service.RegionService;
import com.molichuxing.services.property.GoodsDealerStatusEnum;
import com.molichuxing.services.property.GoodsForbidSaleEnum;
import com.molichuxing.services.property.GoodsTypeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 商品授权相关bff
 * 
 * @author yixuelin
 * @date 2019年8月13日
 *
 */
@Service("goodsAuthBffService")
public class GoodsAuthBffServiceImpl implements GoodsAuthBffService {
	private static final Logger logger = LoggerFactory.getLogger(GoodsAuthBffServiceImpl.class);

	@Resource
	private GoodsAuthService goodsAuthService;

	@Resource
	private DealerService dealerService;

	@Resource
	private RegionService regionService;


	@Override
	public Paged<GoodsDealerVo> getDealerPage(Integer goodsId, String dealerName, Integer cityId, Integer status, int pageNum, int pageSize) {
		List<DealerDto> dealerList = dealerService.getDealerByRegionId(cityId == null?null:Arrays.asList(cityId), dealerName);
		if((null == dealerList)||(dealerList.size() == 0)) {
			return new Paged<>();
		}
		GoodsDealerStatusEnum goodsDealerStatusEnum = status == null?null:GoodsDealerStatusEnum.getName(status);
		Paged<GoodsDealerDto> result = goodsAuthService.getDealerPage(goodsId, getDealerIds(dealerList), goodsDealerStatusEnum, pageNum, pageSize);
		if((null == result)||(null == result.getList())||(result.getList().size() == 0)) {
			return null;
		}
		Map<Integer, DealerDto> dealerMap = null;
		List<DealerDto> dealerDtoList = getDealerDto(result.getList());
		if((null != dealerDtoList)&&(dealerDtoList.size() > 0)) {
			dealerMap = getDealerDto(result.getList()).stream().collect(Collectors.toMap(DealerDto::getDealerId, a -> a,(k1, k2)->k1));
		}

		return Convert.toGoodsDealerPage(result, dealerMap,getDealerAreaName(getDealerDto(result.getList())));
	}

	@Override
	public List<GoodsDealerVo> getDealerList(Integer goodsId, Integer areaId, Integer areaType, Integer status) {
		List<Integer> cityIds = new ArrayList<>();
		if(areaType == 1) {
			List<Integer> regionList = regionService.getCityIdsByProvinceId(areaId);
			for(Integer regionId : regionList) {
				cityIds.add(regionId);
			}
		}else {
			cityIds.add(areaId);
		}
		if(null == cityIds || cityIds.size() ==0) {
			return null;
		}
		List<DealerDto> dealerList = dealerService.getDealerByRegionId(cityIds, null);
		GoodsDealerStatusEnum goodsDealerStatusEnum = status == null?null:GoodsDealerStatusEnum.getName(status);
		List<GoodsDealerDto> result = goodsAuthService.getDealerList(goodsId, getDealerIds(dealerList), goodsDealerStatusEnum);
		/*if((null == result)||(result.size() == 0)) {
			return null;
		}
		Map<Integer, DealerDto> dealerMap = getDealerDto(result).stream().collect(Collectors.toMap(DealerDto::getDealerId, a -> a,(k1, k2)->k1));*/
		return Convert.toGoodsDealerListVo(dealerList, result, null);
	}

	@Override
	public Boolean settingGoodsDistribution(Integer goods, Integer dealerId, Integer status) {
		GoodsDealerStatusEnum goodsDealerStatusEnum = status == null?null:GoodsDealerStatusEnum.getName(status);
		return goodsAuthService.settingGoodsDistribution(goods, dealerId, goodsDealerStatusEnum);
	}

	@Override
	public Boolean createGoodsDealer(Integer goodsId, List<Integer> dealerIds) throws Exception {
		return goodsAuthService.createGoodsDealer(Arrays.asList(goodsId), dealerIds);
	}

	private List<Integer> getDealerIds(List<DealerDto> dealerList) {
		List<Integer> dealers = null;
		if((null != dealerList) && (dealerList.size() > 0)){
			dealers = new ArrayList<>();
			for(DealerDto dealerDto : dealerList) {
				dealers.add(dealerDto.getDealerId());
			}
		}
		return dealers;
	}

	private List<DealerDto> getDealerDto(List<GoodsDealerDto> dealerList) {
        if((null == dealerList) || (dealerList.size() == 0)){
            return null;
        }
        List<Integer> dealers = new ArrayList<>();
        for(GoodsDealerDto dealerDto : dealerList) {
            dealers.add(dealerDto.getDealerId());
        }
		return dealerService.getDealerDtoListByIds(dealers);
	}

	private Map<Integer, String> getDealerAreaName(List<DealerDto> dealerList) {
		Map<Integer, String> dealerMap = null;
		if((null != dealerList) && (dealerList.size() > 0)){
			dealerMap = new HashMap<>();
			for(DealerDto dealerDto : dealerList) {
				RegionInfoDto regionInfoDto = regionService.getCityNameByCityId(dealerDto.getRegionId());
				if(null != regionInfoDto) {
					StringBuilder sb = new StringBuilder();
					sb.append(regionInfoDto.getProvinceFullName()).append(regionInfoDto.getCityFullName());
					dealerMap.put(dealerDto.getRegionId(), sb.toString());
				}
			}
		}
		return dealerMap;
	}
}